// TODO : Serializable, Cloneable, Synchronization
package org.blankie.model.fs;

import java.util.ArrayList;
import java.util.Collection;
import org.blankie.model.BlkAbstractDriver;
import org.blankie.model.DriverOperations;
import org.blankie.util.Organized;

public class BlkFSDriver extends BlkAbstractDriver<BlkFSPath, BlkFileSystem, BlkFolder, BlkFile> {

    public static final long serialVersionUID = 0L;

    private final BlkFSOperations implementation = new BlkFSOperations();
    
    public BlkFSDriver() {
        super();
    }

    public Collection<BlkFolder> getRoots() {
        Collection<BlkFolder> result = new ArrayList<BlkFolder>();
        Collection<BlkFSPath> paths = new ArrayList<BlkFSPath>();
        
        implementation.loadRoots(paths);
        
        for (BlkFSPath path : paths) {
            BlkFolder node = nodeRegistry.search(path);
            
            if (node == null) {
                node = newNode(path);
                nodeRegistry.register(node);
            }
            
            result.add(node);
        }
        
        return result;
    }

    @Override
    protected DriverOperations<BlkFSPath, BlkFileSystem, BlkFolder, BlkFile> getImplementation() {
        return implementation;
    }

    @Override
    protected BlkFile newLeaf(BlkFSPath path) {
        return new BlkFile(this, path);
    }

    @Override
    protected BlkFolder newNode(BlkFSPath path) {
        return new BlkFolder(this, path);
    }

    @Override
    protected BlkFileSystem newOrganization(BlkFSPath path) {
        return new BlkFileSystem(this, path);
    }

    @Override
    public boolean isLeaf(Organized object) {
        return object instanceof BlkFile;
    }

    @Override
    public boolean isNode(Organized object) {
        return object instanceof BlkFolder;
    }

    @Override
    public boolean isOrganization(Organized object) {
        return object instanceof BlkFileSystem;
    }
    
}
